local L = LibStub("AceLocale-3.0"):GetLocale("StatBlock_Durability")

local function CreatePopup()
	local function makeTexture(parent, layer, texture, blend, width, height)
		local t = parent:CreateTexture(nil, layer)
		t:SetTexture(texture)
		if blend then
			t:SetBlendMode(blend)
		end
		if width then
			t:SetWidth(width)
		end
		if height then
			t:SetHeight(height)
		end
		return t
	end

	local function makeFontString(parent, layer, object, text)
		local fs = parent:CreateFontString(nil, layer)
		if object then
			fs:SetFontObject(object)
		end
		if text then
			fs:SetText(text)
		end
		return fs
	end

	local function makeMoneyFrame(parent, offset)
		local f = CreateFrame("Frame", nil, parent)
		f:SetWidth(32)
		f:SetHeight(13)
		local texture = makeTexture(f, "ARTWORK", "Interface\\MoneyFrame\\UI-MoneyIcons")
		texture:SetPoint("RIGHT")
		texture:SetWidth(13)
		texture:SetHeight(13)
		texture:SetTexCoord(offset, offset + 0.25, 0, 1)
		local text = makeFontString(f, "ARTWORK", NumberFontNormal)
		text:SetPoint("RIGHT", texture, "LEFT")
		text:SetHeight(13)
		text:SetJustifyH("RIGHT")
		f.text = text
		return f
	end
	local function makeRepairButton(parent, offset)
		local button = CreateFrame("Button", nil, parent)
		button:SetWidth(32)
		button:SetHeight(32)
		button:SetPushedTexture("Interface\\Buttons\\UI-Quickslot-Depress")

		local texture = makeTexture(button, "BACKGROUND", "Interface\\Buttons\\UI-Quickslot2")
		button:SetPoint("CENTER")
		button:SetNormalTexture(texture)

		texture = makeTexture(button, "BACKGROUND", "Interface\\MerchantFrame\\UI-Merchant-RepairIcons")
		texture:SetTexCoord(offset, offset + 0.28125, 0, 0.5625)
		texture:SetAllPoints()

		button:SetHighlightTexture(makeTexture(button, nil, "Interface\\Buttons\\ButtonHilight-Square"))

		texture = makeTexture(button, nil, "Interface\\MerchantFrame\\UI-Merchant-RepairIcons")
		texture:SetTexCoord(offset, offset + 0.28125, 0, 0.5625)
		texture:SetDesaturated(true)
		button:SetDisabledTexture(texture)
		return button
	end

	local popup = CreateFrame("Frame", nil, UIParent)
	popup:Hide()

	popup:SetFrameStrata("DIALOG")
	popup:EnableMouse(true)
	popup:SetMovable(true)
	popup:SetToplevel()
	popup:SetWidth(256)
	popup:SetHeight(96)
	popup:SetPoint("CENTER")
	popup:SetBackdrop({
		bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true,
		edgeSize = 16,
		tileSize = 32,
		insets = {
			left = 5,
			right = 5,
			top = 5,
			bottom = 5
		}
	})

	local header = CreateFrame("Frame", nil, popup)
	header:SetWidth(192)
	header:SetHeight(32)
	header:SetPoint("TOP", 0, 6)

	local texture = makeTexture(header, "ARTWORK", "Interface\\DialogFrame\\UI-DialogBox-Header", nil, 384, 64)
	texture:SetPoint("TOP", 0, 5)

	local text = makeFontString(header, "ARTWORK", GameFontNormal, L["Repair"])
	text:SetPoint("CENTER", header)

	header:EnableMouse(true)
	header:SetScript("OnMouseDown", function(this)
		this:GetParent():StartMoving()
	end)
	header:SetScript("OnMouseUp", function(this)
		this:GetParent():StopMovingOrSizing()
	end)

	local close = CreateFrame("Button", nil, popup)
	close:SetWidth(32)
	close:SetHeight(32)
	close:SetPoint("TOPRIGHT")
	close:SetNormalTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Up")
	close:SetPushedTexture("Interface\\Buttons\\UI-Panel-MinimizeButton-Down")
	close:SetHighlightTexture(makeTexture(close, nil, "Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight", "ADD"))
	close:SetScript("OnClick", function(this)
		this:GetParent():Hide()
	end)

	local repair = makeRepairButton(popup, 0.28125)
	popup.repair = repair
	repair:SetPoint("TOPRIGHT", -18, -36)
	repair:SetScript("OnClick", function(this)
		StatBlock_Durability:RepairAllItems()
		this:GetParent():Hide()
	end)

	local grepair = makeRepairButton(popup, 0.5625)
	popup.grepair = grepair
	grepair:SetPoint("TOPLEFT", repair, "TOPRIGHT")
	grepair:SetScript("OnClick", function(this)
		StatBlock_Durability:RepairAllItems(1)
		this:GetParent():Hide()
	end)

	local cost = CreateFrame("Frame", nil, popup)
	popup.cost = cost
	cost:SetWidth(96)
	cost:SetHeight(13)
	cost:SetPoint("RIGHT", repair, "LEFT", -8, 0)

	local copper = makeMoneyFrame(cost, 0.5)
	copper:SetPoint("RIGHT")
	local silver = makeMoneyFrame(cost, 0.25)
	silver:SetPoint("RIGHT", copper, "LEFT")
	local gold = makeMoneyFrame(cost, 0)
	gold:SetPoint("RIGHT", silver, "LEFT")

	local label = makeFontString(popup, "ARTWORK", GameFontNormal, L["Total"])
	label:SetPoint("RIGHT", cost, "LEFT", -8, 0)

	function popup:SetValue(value)
		local g = floor(value / 10000)
		local s = mod(floor(value / 100), 100)
		local c = mod(value, 100)
		gold.text:SetText(g)
		silver.text:SetText(s)
		copper.text:SetText(c)
		if g == 0 then
			gold:Hide()
			if s == 0 then
				silver:Hide()
			else
				silver:Show()
			end
		else
			gold:Show()
			silver:Show()
		end
		if CanGuildBankRepair() then
			self.grepair:Show()
			self.repair:ClearAllPoints()
			self.repair:SetPoint("TOPRIGHT", -50, -36)
		else
			self.grepair:Hide()
			self.repair:ClearAllPoints()
			self.repair:SetPoint("TOPRIGHT", -18, -36)
		end
	end

	popup:RegisterEvent("MERCHANT_CLOSED")
	popup:SetScript("OnEvent", function (this)
		this:Hide()
	end)
	local MerchantRepairAllButton = MerchantRepairAllButton
	local prev_handler = MerchantRepairAllButton:GetScript("PostClick")
	local handler
	if prev_handler then
		handler = function (...)
			prev_handler(...)
			popup:Hide()
		end
	else
		handler = function (...)
			popup:Hide()
		end
	end
	MerchantRepairAllButton:SetScript("PostClick", handler)

	return popup
end

function StatBlock_Durability:GetPopup()
	local popup = CreatePopup()
	self.popup = popup
	CreatePopup = nil
	self.GetPopup = function (self) return self.popup end
	return popup
end
